package cz.cvut.fel.gfupan.gui.tab;

import cz.cvut.fel.gfupan.gui.MeasurePanel;
import cz.cvut.fel.gfupan.gui.Session;
import cz.cvut.fel.gfupan.gui.component.Button;
import cz.cvut.fel.gfupan.gui.component.TextArea;
import cz.cvut.fel.gfupan.gui.component.TextField;
import cz.cvut.fel.gfupan.gui.util.Coordinates;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HeaderTab extends AbstractTab {

    private MeasurePanel parent;

    public HeaderTab(MeasurePanel parent) {
        this.parent = parent;
        initComponents();
        fill();
        actualCoords = new Coordinates(0, 0);
        headerNoteField.setFocusTraversalKeysEnabled(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        header = new javax.swing.JPanel();
        fieldsPanel = new javax.swing.JPanel();
        headerDate = new javax.swing.JLabel();
        headerDateField = new TextField(this);
        todayButton = new Button(this);
        headerTemp = new javax.swing.JLabel();
        headerTempField = new TextField(this);
        headerPlaceField = new TextField(this);
        headerAuthorField = new TextField(this);
        headerAuthor = new javax.swing.JLabel();
        headerDeviceField = new TextField(this);
        headerDevice = new javax.swing.JLabel();
        headerPlace = new javax.swing.JLabel();
        headerNote = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        headerNoteField = new TextArea(this);
        jLabel1 = new javax.swing.JLabel();
        backNextPanel = new javax.swing.JPanel();
        headerNextButton = new Button(this);

        setPreferredSize(new java.awt.Dimension(531, 393));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/cvut/fel/gfupan/res/Bundle"); // NOI18N
        headerDate.setText(bundle.getString("MeasurePanel.headerDate.text")); // NOI18N

        headerDateField.setText(bundle.getString("MeasurePanel.headerDateField.text")); // NOI18N

        todayButton.setText(bundle.getString("MeasurePanel.todayButton.text")); // NOI18N
        todayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                todayButtonActionPerformed(evt);
            }
        });

        headerTemp.setText(bundle.getString("MeasurePanel.headerTemp.text")); // NOI18N

        headerTempField.setText(bundle.getString("MeasurePanel.headerTempField.text")); // NOI18N

        headerPlaceField.setText(bundle.getString("MeasurePanel.headerPlaceField.text")); // NOI18N

        headerAuthorField.setText(bundle.getString("MeasurePanel.headerAuthorField.text")); // NOI18N

        headerAuthor.setText(bundle.getString("MeasurePanel.headerAuthor.text")); // NOI18N

        headerDeviceField.setText(bundle.getString("MeasurePanel.headerDeviceField.text")); // NOI18N

        headerDevice.setText(bundle.getString("MeasurePanel.headerDevice.text")); // NOI18N

        headerPlace.setText(bundle.getString("MeasurePanel.headerPlace.text")); // NOI18N

        headerNote.setText(bundle.getString("MeasurePanel.headerNote.text")); // NOI18N

        headerNoteField.setColumns(20);
        headerNoteField.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        headerNoteField.setLineWrap(true);
        headerNoteField.setRows(3);
        jScrollPane1.setViewportView(headerNoteField);

        jLabel1.setText(bundle.getString("HeaderTab.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout fieldsPanelLayout = new javax.swing.GroupLayout(fieldsPanel);
        fieldsPanel.setLayout(fieldsPanelLayout);
        fieldsPanelLayout.setHorizontalGroup(
            fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldsPanelLayout.createSequentialGroup()
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(headerPlace)
                            .addComponent(headerDate)))
                    .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addComponent(headerDevice))
                            .addComponent(headerTemp, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fieldsPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(headerNote)))
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(headerAuthor)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(headerDeviceField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(headerAuthorField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(headerPlaceField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, fieldsPanelLayout.createSequentialGroup()
                                .addComponent(headerDateField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(todayButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, fieldsPanelLayout.createSequentialGroup()
                                .addComponent(headerTempField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        fieldsPanelLayout.setVerticalGroup(
            fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerDate)
                    .addComponent(headerDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(todayButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerAuthorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(headerAuthor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerDeviceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(headerDevice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerTemp)
                    .addComponent(headerTempField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerPlaceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(headerPlace))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addComponent(headerNote)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                .addContainerGap())
        );

        headerNextButton.setText(bundle.getString("MeasurePanel.headerNextButton.text")); // NOI18N
        headerNextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                headerNextButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout backNextPanelLayout = new javax.swing.GroupLayout(backNextPanel);
        backNextPanel.setLayout(backNextPanelLayout);
        backNextPanelLayout.setHorizontalGroup(
            backNextPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backNextPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(headerNextButton)
                .addContainerGap())
        );
        backNextPanelLayout.setVerticalGroup(
            backNextPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backNextPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerNextButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout headerLayout = new javax.swing.GroupLayout(header);
        header.setLayout(headerLayout);
        headerLayout.setHorizontalGroup(
            headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fieldsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(backNextPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        headerLayout.setVerticalGroup(
            headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerLayout.createSequentialGroup()
                .addComponent(fieldsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
                .addComponent(backNextPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(header, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(header, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void todayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_todayButtonActionPerformed
        headerDateField.requestFocus();
        DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
        headerDateField.setText(dateFormat.format(new Date()));
        Session.getInstance().hasChanged(); //zmenili jsme datum
        Session.getInstance().date = dateFormat.format(new Date());
    }//GEN-LAST:event_todayButtonActionPerformed

    private void headerNextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_headerNextButtonActionPerformed
        parent.moveToTab(MeasurePanel.MIRE_TAB);
    }//GEN-LAST:event_headerNextButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backNextPanel;
    private javax.swing.JPanel fieldsPanel;
    private javax.swing.JPanel header;
    private javax.swing.JLabel headerAuthor;
    private javax.swing.JTextField headerAuthorField;
    private javax.swing.JLabel headerDate;
    private javax.swing.JTextField headerDateField;
    private javax.swing.JLabel headerDevice;
    private javax.swing.JTextField headerDeviceField;
    private javax.swing.JButton headerNextButton;
    private javax.swing.JLabel headerNote;
    private javax.swing.JTextArea headerNoteField;
    private javax.swing.JLabel headerPlace;
    private javax.swing.JTextField headerPlaceField;
    private javax.swing.JLabel headerTemp;
    private javax.swing.JTextField headerTempField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton todayButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void clearFields() {
        clearField(headerDateField);
        clearField(headerAuthorField);
        clearField(headerDeviceField);
        clearField(headerTempField);
        clearField(headerPlaceField);
        clearField(headerNoteField);
    }

    private void fill() {
        fillTab(0, headerDateField, todayButton);
        fillTab(1, headerAuthorField);
        fillTab(2, headerDeviceField);
        fillTab(3, headerTempField);
        fillTab(4, headerPlaceField);
        fillTab(5, headerNoteField);
        fillTab(6, headerNextButton);
    }
}
